<?php
/**
 * Created by b17
 * 2/17/12 7:52 PM
 */

class RssController extends \Zend_Controller_Action
{
    protected $baseUrl;

    const NEWS_PER_FEED = 10;

    public function init()
    {
        header('Content-type: text/xml');
        $this->_helper->layout()->disableLayout();
        $this->baseUrl = \Zend_Registry::get('config')->resources->frontController->baseUrl;
    }

    public function __call($name, $args)
    {
        $this->_forward('index');
    }

    public function indexAction()
    {
        $feedData = array(
            'title' => 'Mallards swim team.',
            'description' => 'Competitive swimming.',
            'link' => $this->baseUrl,
            'charset' => 'utf8',
            'entries' => array()
        );
        $newsManager = new \Managers\News();
        $newsManager->setLimit(self::NEWS_PER_FEED);
        $news = $newsManager->getRecent();
        foreach ($news as $article) {
            $link = "/todo";
            $entry = array(
                'title' => $article['title'],
                'description' => strip_tags($article['content']),
                'link' => $link,
                'lastUpdate' => $article['date']->getTimestamp(),
            );
            $feedData['entries'][] = $entry;
        }
        $feed = \Zend_Feed::importArray($feedData, 'rss');
        $this->view->feed = $feed->send();
    }


}
